<?php
	//Pagination Partie 1
	$req = $bdd->query('SELECT COUNT(id) AS nombre_articles_total FROM articles');
	$donnees = $req->fetch();
	$nombre_articles_total=$donnees['nombre_articles_total'];

	$nombre_pages=ceil($nombre_articles_total/$nombre_articles_page);
	if ((isset($_GET['numero_page'])) && ($_GET['numero_page']>0) && ($_GET['numero_page']<=$nombre_pages)){
		$page_courante = $_GET['numero_page'];
	}
	else{
		$page_courante = 1;
	}
	$article_debut=(($page_courante-1)*$nombre_articles_page);
	
	if ((isset($_SESSION['identifiant'])) AND (isset($_SESSION['groupe'])==2))
	{
		
		$req = $bdd->query('SELECT * FROM articles ORDER BY id LIMIT '. $article_debut.', '.$nombre_articles_page.'');
		?>
		<h1 id="titre">Gestion des articles</h1>
		<form method="post" action="page.php?page=admin_articles">
			<table>
				<tr>
				<td><label for="nombre_articles">Nombre d'articles par page</label></td>
				<td><input type="number" name="nombre_articles" value="<?php echo $nombre_articles_page; ?>" maxlength="10" size="10" min="1" max="20" step="1"></td>
				<td><input type="submit" value="Sauver" name="editerpage" /></td>
				</tr>
		</form>

		<?php
		while ($donnees = $req->fetch())
		{
			?>
			<form method="post" action="page.php?page=admin_articles">
				
				<tr>
				<td><?php echo $donnees['titre']; ?></td>
				<td><?php echo $donnees['date_creation']; ?><input type="hidden" name="id" value="<?php echo $donnees['id']; ?>" /></td>
				<td><input type="submit" value="<?php echo EDITER; ?>" name="editer" /><input type="submit" value="<?php echo EFFACER; ?>" name="effacer" /></td>
				</tr>
				
			</form>
			<?php 
		}
		?>
		</table>
		<?php
		$req->closeCursor();
		
		?>
		<ul id="pagination">
			<?php
				$suiv = $page_courante + 1; 
				$prec = $page_courante - 1;  
				if ($nombre_pages > 1 && $page_courante > 1 ) {
					if($page_courante == 1 ) {
						?>
						<li class="previous-off"> <<< </li>
						<?php 
					} 
					else  {
						?>
						<li class="previous"><a href="page.php?page=admin_articles&numero_page=<?php echo $prec; ?>"> <<< </a></li>
						<?php    
					}
				}
				for($i=1; $i<=$nombre_pages; $i++) 
				{ 
					if($i==$page_courante) { 
						?>
						<li class="active"><?php echo $i; ?></li>
						<?php
					} 
					else { 
						?>
						<li><a href="page.php?page=admin_articles&numero_page=<?php echo $i; ?>"><?php echo $i; ?></a></li>
						<?php
					} 
				} 
				if ($nombre_pages > 1 && $page_courante < $nombre_pages  ) {
					if($page_courante == $nombre_pages - 1 ) {
						?>
						<li class="next"><a href="page.php?page=admin_articles&numero_page=<?php echo $suiv; ?>"> >>> </li>
						<?php 
					} 
					else {
						?>
						<li class="next-off"> >>> </li>
						<?php
					}
				}
			?>
		</ul>
		
		<?php
		if (isset($_POST['editer'])) 
		{
			$req = $bdd->prepare('SELECT * FROM articles WHERE id = :id');
			$req->execute(array(
				'id' => $_POST['id']
			));
			$donnees = $req->fetch();
			
			?>
			<h1><?php echo TITRE_EDITER_ARTICLE; ?></h1>
			<form method="post" action="page.php?page=admin_articles">
			<table>
				<tr>
				<td><label for="titre">Titre</label><input type="hidden" name="id" value="<?php echo $donnees['id']; ?>" required /></td>
				<td><input type="text" name="titre" value="<?php echo $donnees['titre']; ?>" maxlength="250" size="50"></td>
				<td><input type="submit" value="<?php echo MODIFIER_ARTICLE; ?>" name="modifier" /></td></tr>
				<tr><td colspan="3">
				<textarea id="article" name="article" rows="6" cols="85"><?php echo $donnees['article']; ?></textarea></td></tr>
				</table>
			</form>
			<?php
		}
		elseif (isset($_POST['modifier'])) 
		{
			$req = $bdd->prepare('UPDATE articles SET titre = :titre, article = :article, date_modification = :date_modification WHERE id=:id');
			$req->execute(array(
				'titre' => $_POST['titre'],
				'article' => $_POST['article'],
				'date_modification' => date ('j m Y'),
				'id' => $_POST['id']
			));
			header('Location: page.php?page=admin_articles'); 
		}
		elseif (isset($_POST['effacer'])) 
		{
			$req = $bdd->prepare('DELETE FROM articles WHERE id=:id');
			$req->execute(array(
					'id' => $_POST['id']
			));
			header('Location: page.php?page=admin_articles'); 
		}
		elseif (isset($_POST['ajouter'])) 
		{
			$req = $bdd->prepare('INSERT INTO articles (titre, article, date_modification) VALUES (:titre, :article, :date_modification)');
			$req->execute(array(
				'titre' => $_POST['titre'],
				'article' => $_POST['article'],
				'date_modification' => date ('j m Y')
			));
			header('Location: page.php?page=admin_articles');
		}
		elseif (isset($_POST['editerpage'])) 
		{
			$req = $bdd->prepare('UPDATE admin_config SET nombre_articles = :nombre_articles WHERE id=:id');
			$req->execute(array(
				'nombre_articles' => $_POST['nombre_articles'],
				'id' => 0
			));
			header('Location: page.php?page=admin_articles'); 
			//echo 'TEST '. $_POST['nombre_articles'];
		}
		
		?><hr />
		<details>
		   <summary><?php echo TITRE_AJOUTER_ARTICLE; ?></summary>
		   <form method="post" action="page.php?page=admin_articles">
				<table>
					<tr>
					<td><label for="titre">Titre</label></td>
					<td><input type="text" name="titre" value="" maxlength="250" size="50" required></td>
					<td><input type="submit" value="<?php echo AJOUTER_ARTICLE; ?>" name="ajouter" /></td></tr>
					<tr><td colspan="3">
					<textarea id="article" name="article" rows="10" cols="70"> </textarea>
					</td></tr>
				</table>
			</form>
		</details>
		
		
		<?php
	}
	else
	{
		echo NON_ACCES_ADMIN;
	}
?>